/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.startup.musicstore.services.crud.Impl;

import com.startup.musicstore.model.Customer;
import com.startup.musicstore.model.CustomerInvoice;
import com.startup.musicstore.model.Orders;
import com.startup.musicstore.services.crud.CustomerCrudService;
import com.startup.musicstore.services.crud.OrderCrudService;
import com.startup.musicstore.services.crud.UnpaidService;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 *
 * @author given
 */
public class UnpaidServiceImpl implements UnpaidService{
    private static ApplicationContext ctx;
    @Override
    public String getUnpaidOrders() {
        ctx = new ClassPathXmlApplicationContext("classpath:com/startup/musicstore/app/conf/applicationContext-*.xml");
        OrderCrudService orderCrudService = (OrderCrudService) ctx.getBean("orderCrudService");
        List<Orders> orders = orderCrudService.findAll();
        for(Orders o: orders){
            CustomerInvoice customerInvoice = o.getCustomerInvoice();
            if(customerInvoice.getInvoiceStatus().equalsIgnoreCase("Unpaid")){
                return "unpaid";
            }
        }
        return "paid";
    }

    @Override
    public String getUnpaidCustomers() {
        ctx = new ClassPathXmlApplicationContext("classpath:com/startup/musicstore/app/conf/applicationContext-*.xml");
        CustomerCrudService customerCrudService = (CustomerCrudService) ctx.getBean("customerCrudService");
        List<Customer> customers = customerCrudService.findAll();
        for(Customer c: customers){
            List<Orders> orders = c.getOrders();
            for(Orders o: orders){
                CustomerInvoice customerInvoice = o.getCustomerInvoice();
                if(customerInvoice.getInvoiceStatus().equalsIgnoreCase("Unpaid")){
                    return "unpaid";
                }
            }
        }
        return "paid";
    }    
}
